from apps.codetable.filters import CodeTableFilter
from apps.codetable.models import CodeTableDetail
from apps.codetable.serializers import CodeTableSerializer
from bases.viewsets import APIReadOnlyViewSet
from rest_framework.views import APIView
from django.utils import translation
from bases.response import SuccessResponse


class CodeTableViewSet(APIReadOnlyViewSet):
    def get_queryset(self):
        # 暂时只使用中文设定
        return CodeTableDetail.objects.filter(codetable_head__localization='zh-hans').order_by('id')

    serializer_class = CodeTableSerializer
    pagination_class = None

    filter_class = CodeTableFilter

class CodeTableDetailView(APIView):
    def get(self,request, model, field):
        localization = translation.get_language()

        if model=="EMPTY":
            data = CodeTableDetail.objects.filter(codetable_head__localization=localization, codetable_head__model__isnull=True, codetable_head__field=field)
        else:
            data = CodeTableDetail.objects.filter(codetable_head__localization=localization, codetable_head__model=model, codetable_head__field=field)
        serializer = CodeTableSerializer(data, many=True)
        return SuccessResponse(serializer.data)
